package ch02.test;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class test2 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        //得到DOm解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即DOM树
        Document doc = db.parse("src/ch02/entity/demo2.xml");



        //读取pubDate
        NodeList list1 =doc.getElementsByTagName("title");
       // NodeList list2 =doc.getElementsByTagName("description");
        NodeList list = doc.getElementsByTagName("pubDate");

        //pubDae元素节点
        Element titleElement = (Element)list1.item(0);
       // Element descriptionElement = (Element)list2.item(1);
        Element pubDateElement = (Element)list.item(0);

        //读取文本节点
        String title = titleElement.getFirstChild().getNodeValue();
        System.out.println(title);
//        String description = titleElement.getFirstChild().getNodeValue();
//        System.out.println(description);
        String pubDate = pubDateElement.getFirstChild().getNodeValue();
        System.out.println(pubDate);


    }
}
